package com.song.controller.role;

import com.song.common.Result;
import com.song.controller.BaseController;
import com.song.entity.role.Role;
import com.song.service.role.RoleService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("${api.version}/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/roleManage")
    public String roleManage() {
        return "role/roleManage";
    }


    /**
     * 获取角色列表
     */
    @RequestMapping("/roleList")
    @ResponseBody
    public Result<List<Role>> roleList( Role role, String startTime, String endTime,
            @RequestParam(value = "page", defaultValue = "0") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Result<List<Role>> result  =  roleService.selectRoleList(role,startTime,endTime,pageNum,pageSize);
        return result;
    }

    /**
     *  根据用户ID删除角色
     */
    @RequestMapping("/deleteRoleById")
    @ResponseBody
    public Result<Integer> deleteRoleById(int roleId) {
        Result<Integer> result =  roleService.deleteRoleById(roleId);
        return result;
    }

    /**
     * 批量删除角色
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public Result<Integer> batchDel(String ids) {
        String[] array = ids.split(",");
        Result<Integer> result =  roleService.batchDel(array);
        return result;
    }

    /**
     * 新增角色
     */
    @RequestMapping("/addRole")
    @ResponseBody
    public Result<Integer> addRole(Role role) {
        Result<Integer> result =  roleService.addRole(role);
        return result;
    }

    /**
     * 更新角色
     */
    @RequestMapping("/updateRole")
    @ResponseBody
    public Result<Integer> updateRole(Role role) {
        Result<Integer> result =  roleService.updateRole(role);
        return result;
    }

    /**
     * 根据用户ID,获得角色树
     */
    @RequestMapping("/getRoleTreeByUserId")
    @ResponseBody
    public JSONArray getRoleTreeByUserId(Integer userId) {
        return roleService.getRoleTreeByUserId(userId);
    }

    /**
     * 获得整个角色树 (ztree实现)
     */
    @RequestMapping("/getRoleTree")
    @ResponseBody
    public JSONArray getRoleTree() {
        return roleService.getRoleTree();
    }

}
